package com.framework.apollo;



import com.github.wnameless.json.flattener.JsonFlattener;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.xml.XMLSerializer;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;

import java.util.*;

public class test {
    public static void main(String[] args) {
//        String jsonStr="{\"apexini\":{\"_Version\":\"1.0\",\"_comment\":\"ApexSoft Config Data\",\"SERVER\":{\"BufSize\":{\"_Value\":\"4096\"},\"Free\":{\"_Value\":\"5000\"},\"MediumBufSize\":{\"_Value\":\"131072\"},\"MediumFree\":{\"_Value\":\"3\"},\"MaxBufSize\":{\"_Value\":\"819200000\"},\"Server1\":{\"_Port\":\"${RS_PORT}\",\"_SSL\":\"0\",\"_MaxSocket\":\"100\",\"_ListenNum\":\"100\",\"_VerifyClient\":\"1\"}},\"ProcThreadPool\":{\"Proc\":{\"TPNUM_INIT\":{\"_Value\":\"4\"}}},\"SYSTEM\":{\"AutoRun\":{\"_Value\":\"0\"},\"AutoRestart\":{\"_Value\":\"1\"},\"ReqTimeOut\":{\"_Value\":\"10\"}},\"NAMESERVICE\":{\"REGION\":{\"_Value\":\"${A5_NAME}rs\"},\"NAME\":{\"_Value\":\"${A5_NAME}rs_${A5_IPNUM}\"}},\"LOG\":{\"WRITELOG\":{\"_Value\":\"0\"},\"TYPE\":{\"_Value\":\"0\"},\"MODE\":{\"_Value\":\"0\"}},\"STAT\":{\"USED\":{\"_Value\":\"1\"}},\"AxSvr\":{\"file_svr\":{\"Server\":{\"_LinkAddr\":\"${LOCAL_IP}@${FILE_SVR_PORT}/tcp\",\"_num\":\"1\",\"_ssl\":\"0\",\"_type\":\"0\",\"_CheckLan\":\"1\",\"_CheckLanSec\":\"2\",\"_CheckLanTimeOut\":\"15\"}}}}}";
//        JSONObject json2 = JSONObject.fromObject(jsonStr);
//        Map<String, Object> flatMap = JsonFlattener.flattenAsMap(json2.toString());
//        System.out.println(flatMap);
//        String xmlStr=json2xml(jsonStr);
//        System.out.println(xmlStr);
//        String xmlStr="<?xml version=\"1.0\" encoding=\"gb2312\" standalone=\"no\" ?>\n" +
//                "<apexini Version=\"1.0\" comment=\"ApexSoft Config Data\">\n" +
//                "<SERVER>\\r\\n<BufSize Value=\"4096\"/>\n" +
//                "<Free Value=\"5000\"/>\n" +
//                "<MediumFree Value=\"3\"/>\n" +
//                "</SERVER>\n" +
//                "</apexini>";
//        XMLSerializer xmlSerializer = new XMLSerializer();
//        xmlSerializer.setTypeHintsEnabled(false);
//        String resutStr = xmlSerializer.read(xmlStr).toString(0);//0表示去除换行空格等，1以上表示json格式化后的数据
//        System.out.println(resutStr);
    }

    /**
     * JSON(数组)字符串转换成XML字符串
     * （必须引入 xom_1.1.jar）
     * @param jsonString
     * @return
     */
    public static String json2xml(String jsonString) {
        StringBuffer sb = new StringBuffer();
        String xmlStr = "";

        XMLSerializer xmlSerializer = new XMLSerializer();
        xmlSerializer.setTypeHintsEnabled(false);
        String xml=xmlSerializer.write(JSONSerializer.toJSON(jsonString));
        xml = xml.replace("<o>", "").replace("</o>", "");
        xml = xml.replaceAll("\r\n", "").concat("\r\n");
        Document document;

        try {
            document = DocumentHelper.parseText(xml);
            xmlStr = document.getRootElement().asXML();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
                .append(xmlStr);
        return sb.toString();
    }


}
